# -*- coding: UTF-8 -*-
# __author__ = '10459'

# 从原始数据到达实际页面展示数据就是要建立一个viewmodel模型，非数据库
from app.view_models.book import BookViewModel


class TradeInfo:
    def __init__(self, goods):
        self.total = 0
        self.trades = []
        self.__parse(goods)

    def __parse(self, goods):
        self.total = len(goods)
        self.trades = [self.__map_to_trade(single) for single in goods]

    def __map_to_trade(self, single):
        if single.create_datetime:
            time=single.create_datetime.strftime('%Y-%m-%d')
        else:
            time='未知时间'

        # single.create_datetime.strftime('%Y-%m-%d') if single.create_datetime else '未知时间'
        return dict(user_name=single.user.nickname,time=time,id=single.id)

class MyTrades:
    def __init__(self,trades_of_mine,trades_count_list):
        self.trades=[]
        self.__trades_of_mine= trades_of_mine
        self.__trades_count_list = trades_count_list
        #不建议在自己内部的函数中去改变属性的值，可以查询读取，可以通过函数调用增加中间参数如temp_gifts等，改变后然后作为返回值再赋值给属性
        #不然不知道哪个属性在哪个方法被修改
        self.trades = self.__parse()
    def __parse(self):
        temp_trades=[]
        for gift in self.__trades_of_mine:
            my_gift = self.__matching(gift)
            temp_trades.append(my_gift)
        return temp_trades
    def __matching(self,trade):
        count=0
        for trade_count in self.__trades_count_list:
            if trade.isbn==trade_count["isbn"]:
                count= trade_count["count"]

        my_trade={
            "id":trade.id,
            "book":BookViewModel(trade.book),
            "trades_count":count
        }

        return my_trade